import { DataCenter } from './../data/DataCenter';
/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-13 15:04:52
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-27 23:46:42
 * @LastEditors: judu233
 */
import { Root } from "./Root";
import c3d from "../tool/cc";
import { AudioManager } from "../audio/AudioManager";
import { GameManager } from "./Manager/GameManager";
import { HttpRequest } from "../NetWork/HttpRequest";
import { TimerManager } from "./Manager/TimerManager";
import { Config, ConfigData } from "../config/Config";
import EventBroadcast from "./Manager/EngineMessage";
import LanguageManager from "./Manager/LanguageManager";
import { _AssetManager } from "../Asset/AssetManager";
import { UIManager } from "../Asset/ui/UIManager";
import { StorageUtil } from "../tool/storage/StorageUtil";
import { EntryManager } from "../entry/EntryManager";
import { LogicManager } from "./Manager/LogicManager";
import UIView from "../Asset/ui/UIView";
import { CmmEntry } from '../../com/script/entry/CmmEntry';

type T_GM = typeof GM;
declare global {
    let GM: T_GM;
}

class GM {
    /** 多语言模块 */
    static i18n: LanguageManager;
    /** 游戏时间管理 */
    static timer: TimerManager;
    /** 游戏音乐管理 */
    static audio: AudioManager;
    /** 三维游戏世界管理 */
    static game: GameManager;
    /**config */
    static config: ConfigData;
    /**事件 */
    static eventMgr: EventBroadcast;
    /**ui管理 */
    static uiMgr: UIManager;
    /**资源加载 */
    static assetMgr: _AssetManager;
    /**本地存储 */
    static storage: StorageUtil;
    /**游戏模块管理*/
    static entryMgr: EntryManager;
    /**逻辑管理器 */
    static logic: LogicManager;
    /**数据管理器 */
    static dataMgr: DataCenter;
    /** 老版本HTTP */
    static http: HttpRequest;

    /**获取当前游戏的GameView */
    static getGameView<T extends UIView>() { return <T>this.uiMgr.curUIView; }

    /** 在Root.ts中初始化引导模块 */
    static init(root: Root) {
        GM.eventMgr = new EventBroadcast();
        GM.assetMgr = new _AssetManager();
        GM.entryMgr = new EntryManager();
        GM.dataMgr = new DataCenter();
        GM.storage = new StorageUtil();
        GM.uiMgr = new UIManager();
        GM.i18n = new LanguageManager()
        GM.timer = new TimerManager(root);
        GM.audio = new AudioManager();
        GM.game = root.addComponent(GameManager)!;
        GM.http = new HttpRequest();
        GM.config = new ConfigData();

        this.uiMgr.adaptor.initBrowserAdaptor();
        this.assetMgr.hotupdate.commonHotUpdateUrl = Config.TEST_HOT_UPDATE_URL_ROOT;
        this.assetMgr.hotupdate.isSkipCheckUpdate = Config.isSkipCheckUpdate;

        //初始化自定主entry代理
        this.entryMgr.delegate = new CmmEntry();

        //语言包初始化
        // this.language.addSourceDelegate(new CommonLanguage);

        this.uiMgr.adaptor.onLoad(root.node);
        //预先加载下loading预置体
        this.uiMgr.tip.preloadPrefab();
        // this.uiMgr.uiLoading.preloadPrefab();
        this.uiMgr.loading.preloadPrefab();
        this.uiMgr.alert.preloadPrefab();
        //Service onLoad
        // this.serviceManager.onLoad();
        //入口管理器
        this.entryMgr.onLoad(root.node);


        //设置游戏
        c3d.game.frameRate = 60;
        // 游戏显示事件
        c3d.game.on(c3d.Game.EVENT_SHOW, () => {
            c3d.log("Game.EVENT_SHOW");
            GM.timer.load();     // 平台不需要在退出时精准计算时间，直接暂时游戏时间
            GM.audio.resumeAll();
            c3d.director.resume();
            c3d.game.resume();
            GM.eventMgr.ins.stickyBroadcast('GAME_ENTER');
        });

        // 游戏隐藏事件
        c3d.game.on(c3d.Game.EVENT_HIDE, () => {
            c3d.log("Game.EVENT_HIDE");
            GM.timer.save();     // 平台不需要在退出时精准计算时间，直接暂时游戏时间
            GM.audio.pauseAll();
            c3d.director.pause();
            c3d.game.pause();
            GM.eventMgr.ins.stickyBroadcast('GAME_EXIT');
        });

        // 游戏尺寸修改事件
        c3d.view.setResizeCallback(() => {
            c3d.log("Game.setResizeCallback");
            GM.eventMgr.ins.stickyBroadcast('GAME_RESIZE');
        });
    }

    /** 修改引擎全局游戏速度  */
    static get speed(): number {
        return c3d.kGetSpeed();
    }
    static set speed(value: number) {
        c3d.kSetSpeed(value);
    }
}


(<any>window)["GM"] = GM;